package com.goparry.taskman.inbox;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.goparry.taskman.R;
import com.goparry.taskman.browse.DocumentActivity;
import com.goparry.taskman.browse.DocumentFragment;
import com.goparry.taskman.repository.Task;
import com.goparry.taskman.restclient.AsyncRestClientTasks;
import com.goparry.taskman.restclient.RESTUtilities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

/**
 * @author davidp
 * Activity for managing all aspects of a user's inbox.
 * 
 */
public class InboxActivity extends FragmentActivity implements InboxFragment.InboxListeners {
	private List<Task> mTasks;
	
    @Override
	public void onCreate(Bundle icicle){ 
		super.onCreate(icicle); 
		
		// Get the user's list of tasks from the server
		// The request is executed on its own thread using AsyncTask
		// As this the resulting date is required before the activity is displayed
		// we have to wait until execution is complete and we can get the results.
		RESTUtilities.setServer(RESTUtilities.SERVER.GAE);
		AsyncRestClientTasks arct = new AsyncRestClientTasks(this);
		arct.execute();
		try {
			mTasks = arct.get(20, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// Set the layout for this tab to inbox.xml
		setContentView(R.layout.inbox);
	}
	@Override
	public List<Task> onInitialLoad() {
		return mTasks;
	}
	@Override
	public void onTaskSelected(Task task) {
		// This handles the selection of an item in the ListView

        // Create a Bundle to pass the Task data to the Task Dialogue
        Bundle taskBundle = new Bundle();
        taskBundle.putString("name", task.getName());
        taskBundle.putString("title", task.getTitle());
        taskBundle.putString("instructions",task.getInstructions());
        taskBundle.putString("step", task.getStep());
        taskBundle.putString("received_date", task.getReceivedDate());
        taskBundle.putString("due_date", task.getDueDate());
        taskBundle.putString("type", task.getType());
        taskBundle.putString("documents", task.getDocumentIDs("-"));
        taskBundle.putInt("number_of_docs", task.getNumberOfDocs());
        
		// Check if the TaskFragment exists and is in the current layout.
        // If it's not then start the TaskFragment in its own TaskActivity.
        // If it does exist then call its updateDocument() method to display the document details.
        TaskFragment taskFragment = (TaskFragment) getSupportFragmentManager()
				.findFragmentById(R.id.taskfragment);
        if (taskFragment == null || !taskFragment.isInLayout()) {
            //Create an Intent to launch the document Dialogue for this task
            Intent intent = new Intent().setClass(getApplicationContext(), TaskActivity.class);
            
            intent.putExtras(taskBundle);
            
            //Run the Activity
            startActivity(intent);       	
        } else {
        	taskFragment.updateTask(taskBundle);
        }
	}
}
